package com.myproject.myblogserver.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myproject.myblogserver.contant.CommonConstants;
import com.myproject.myblogserver.entity.Like;
import com.myproject.myblogserver.service.ILikeService;
import com.myproject.myblogserver.service.IMyBlogPreviewService;
import com.myproject.myblogserver.utils.JwtUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 点赞表 前端控制器
 * </p>
 *
 * @author jiangweike
 * @since 2025-02-16
 */
@Controller
@RequestMapping("/like")
public class LikeController {

    @Autowired
    private ILikeService likeService;

    @Autowired
    private IMyBlogPreviewService myBlogPreviewServiceImpl;

    @PostMapping("/add/{targetBlogId}")
    public ResponseEntity<?> add(@PathVariable Long targetBlogId,
            @RequestHeader("Authorization") String authHeader) {
        try {
            var userId = JwtUtil.extractUserIdFromAuthHeader(authHeader);
            var like = new Like();
            like.setUserId(userId);
            like.setBlogId(targetBlogId);
            likeService.save(like);
            return ResponseEntity.ok("点赞成功!");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("点赞失败！");
        }
    }

    @PostMapping("/delete/{targetBlogId}")
    public ResponseEntity<?> delete(@PathVariable Long targetBlogId,
            @RequestHeader("Authorization") String authHeader) {
        try {
            var userId = JwtUtil.extractUserIdFromAuthHeader(authHeader);
            var like = likeService.lambdaQuery()
                    .eq(Like::getIsDeleted, CommonConstants.DELETED_STATUS_NO)
                    .eq(Like::getBlogId, targetBlogId)
                    .eq(Like::getUserId, userId)
                    .one();
            likeService.updateById(like);
            return ResponseEntity.ok("取消点赞成功!");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("取消点赞失败！");
        }
    }

    @PostMapping("/getLikes/{pageIndex}")
    public ResponseEntity<?> getLikes(@PathVariable Integer pageIndex,
            @RequestHeader("Authorization") String authHeader) {
        try {
            var userId = JwtUtil.extractUserIdFromAuthHeader(authHeader);
            var likes = likeService.lambdaQuery()
                    .eq(Like::getIsDeleted, CommonConstants.DELETED_STATUS_NO)
                    .eq(Like::getUserId, userId)
                    .select(Like::getBlogId, Like::getCreatedAt)
                    .page(new Page<>(pageIndex, CommonConstants.DEFAULT_PAGE_SIZE));

            var blogIds = likes.getRecords().stream().map(like -> like.getBlogId()).toList();
            var pageResultBlogPreview = myBlogPreviewServiceImpl.getPageResultBlogPreviewByIds(blogIds, pageIndex);

            return ResponseEntity.ok(pageResultBlogPreview);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("获取已点赞博客失败！");
        }
    }
}
